###########################################################################
###		Application Make include file				###
###   									###
###  app.mk								###
###									###
###########################################################################

###################################################
### Imported variables:				###
###						###
### TARGET =					###
### OBJECTS = 					###
### PROGFLAGS = 				###
### PROGLIBS =					###
### DISTFILES =					###
### LIBPROGS = 					###
### PROGDEFS = 					###
### PRE = 					###
###################################################

#################################################
# before including this file:			#
#						#
#include ../common.mk				#
#APPTARGET := $(bindir)/$(TARGET)		#
#all: $(APPTARGET)				#
#################################################

LIBS = 

DEFS   +=  $(PROGDEFS)
CFLAGS +=  $(PROGFLAGS) -I${PROJECT_BASE}

APPENVS += LD_LIBRARY_PATH=${OPENSOURCE_LIB} GLOG_log_dir=/tmp/$(PROJECT_NAME)/

ifeq ($(profile),on)
PROFILE = -pg
else
PROFILE = 
endif

ifneq ($(pure),)
  CC      = gcc
  CPP     = gcc
  LD      = $(CPP)
else
  PURIFY  =  
endif

ifeq ($(os),Solaris)
  PROGLIBS += -lsocket -lnsl -ldl
endif
ifeq ($(os),Solaris6)
  PROGLIBS += -lsocket -lnsl -ldl
endif
ifeq ($(os),Solaris7)
  PROGLIBS += -lsocket -lnsl -ldl
endif

ifeq ($(os), Solaris8)
  PROGLIBS += -lsocket -lnsl -ldl
endif

ifeq ($(os), Solaris9)
  PROGLIBS += -lsocket -lnsl -ldl
endif

ifeq ($(os),SolarisPC)
  PROGLIBS += -lsocket -lnsl -ldl
endif

ifeq ($(os),UNIX_SV)
  PROGLIBS += -lsocket -lnsl -ldl -lcrypt -lgen
endif

# Linking rules.
link_command = $(PURIFY) $(LD) $(LDFLAGS) -o $@ $(PROFILE)

.SUFFIXES=
.SUFFIXES= $(objdir)/%.o .c .o .C .cpp .cc

OBJ_OUT = $(foreach file, $(OBJECTS) $(CPPOBJECTS) $(CCOBJECTS), $(objdir)/$(file))
SRC_OUT = $(OBJECTS:.o=.c) $(CPPOBJECTS:.o=.cpp) $(CCOBJECTS:.o=.cc)
LIB_OUT = $(foreach file, $(LIBLIST), $(libdir)/lib$(file).a)
INC = -I$(srcdir) -I. -I.. -I$(incdir)

# dependencies
TMP = $(filter %.o, $(OBJ_OUT))

ifneq ($(SUNWS),on)
  ifneq ($(OS),Windows_NT)
    DEP_OUT = $(TMP:.o=.d)
    ifeq ($(OS),HP-UX)
  	include $(DEP_OUT)
    else
      sinclude $(DEP_OUT)
    endif


    $(objdir)/%.d: %.c $(objdir)/null
	$(SHELL) -ec "$(CC) -M $(INC) $(DEFS) $(CFLAGS) $(CPPFLAGS) -c $< | sed 's/$*\.o/$(subst /,\/,$(objdir)/$*.o) $(subst /,\/,$(objdir)/$*.d)/g' > $@"

    $(objdir)/%.d: %.C $(objdir)/null
	$(SHELL) -ec "$(CC) -M $(INC) $(DEFS) $(CFLAGS) $(CPPFLAGS) -c $< | sed 's/$*\.o/$(subst /,\/,$(objdir)/$*.o) $(subst /,\/,$(objdir)/$*.d)/g' > $@"

    $(objdir)/%.d: %.cpp $(objdir)/null
	$(SHELL) -ec "$(CPP) -M $(INC) $(DEFS) $(CFLAGS) $(CPPFLAGS) -c $< | sed 's/$*\.o/$(subst /,\/,$(objdir)/$*.o) $(subs /,\/,$(objdir)/$*.d)/g' > $@"

    $(objdir)/%.d: %.cc $(objdir)/null
	$(SHELL) -ec "$(CPP) -M $(INC) $(DEFS) $(CFLAGS) $(CPPFLAGS) -c $< | sed 's/$*\.o/$(subst /,\/,$(objdir)/$*.o) $(subs /,\/,$(objdir)/$*.d)/g' > $@"
  endif
endif

$(objdir)/%.o: %.cpp
	$(CPP) $(CFLAGS) -c $< $(CPPFLAGS) $(DEFS) $(INC) -o $@

$(objdir)/%.o: %.cc
	$(CPP) $(CFLAGS) -c $< $(CPPFLAGS) $(DEFS) $(INC) -o $@

$(objdir)/%.o: %.c 
	$(CC) $(CFLAGS) -c $< $(CPPFLAGS) $(DEFS) $(INC) -o $@

$(APPTARGET): $(OBJ_OUT) $(LIB_OUT) $(DEP_OUT) $(bindir)/null $(objdir)/null
	$(link_command) $(OBJ_OUT) $(LIB_OUT) $(LINKCMD) -L$(libdir) -L$(OPENSOURCE_LIB) $(PROGLIBS) $(LINKCMDENDOFLINE)

subdir = src

check:

ifeq ($(SHELL_TYPE),dos)
%/null:
	IF NOT EXIST "$(dir $@)" MKDIR "$(dir $@)"
	IF NOT EXIST "$@" ECHO null > "$@"

else
%/null: 
	if ( test ! -f $@) ; then \
		mkdir -p $(dir $@); \
		echo "" > $@; \
	fi
endif

lint:
	$(LINT) $(LINT_FLAGS) $(DEFS) $(INC) $(CFLAGS) $(SRC_OUT)

clean:
rm:
	$(RM) -f $(APPTARGET) *.o core $(OBJ_OUT) $(DEP_OUT)
rmt:
	$(RM) -f $(APPTARGET) core

mostlyclean: clean

depend:
	makedepend *.c *.C *.cpp *.cc $(INC) $(PROGFLAGS)


distclean: clean
	$(RM) -f Makefile

realclean: distclean
	$(RM) -f TAGS

distdir = ../`cat ../distname`/$(subdir)
dist: $(DISTFILES)
	for file in $(DISTFILES); do \
	  ln $$file $(distdir) \
	    || { echo copying $$file instead; cp -p $$file $(distdir);}; \
	done

version:
	@echo Version:$(VERSION) By $(AUTHOR)

run: all
	$(APPENVS) $(APPTARGET) $(APPARGS)

gdb: all
	$(APPENVS) gdb $(APPTARGET)

gdbrun: all
	$(APPENVS) gdb $(APPTARGET)

valgrind: all
	$(APPENVS) valgrind --leak-check=full $(APPTARGET) $(APPARGS)

# Tell versions [3.59,3.63) of GNU make not to export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:

